package com.gash.scrape.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class Page {
	private String title;
	private String url;
	private ArrayList<String> metadata;
	private HashMap<String, Link> links;
	private HashMap<String, PageWord> keywords;

	public void addMetadata(String v) {
		if (metadata == null)
			metadata = new ArrayList<String>();

		if (!metadata.contains(v))
			metadata.add(v);
	}

	public void addLink(Link v) {
		if (v == null)
			return;
		else if (links == null)
			links = new HashMap<String, Link>();

		if (links.containsKey(v.getUrl())) {
			Link l = links.get(v.getUrl());
			l.merge(v);
		} else
			links.put(v.getUrl(), v);
	}

	public void addKeyword(String word) {
		if (word == null)
			return;
		else if (keywords == null)
			keywords = new HashMap<String, PageWord>();

		String lw = word.trim().toLowerCase();
		PageWord pw = keywords.get(lw);
		if (pw == null) {
			pw = new PageWord(lw);
			keywords.put(pw.getWord(), pw);
		} else
			pw.increment();
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public ArrayList<String> getMetadata() {
		return metadata;
	}

	public HashMap<String, Link> getLinks() {
		return links;
	}

	public HashMap<String, PageWord> getKeywords() {
		return keywords;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Title: " + title);
		sb.append("\nURL: " + url);
		sb.append("\nKeywords: ");
		if (keywords != null) {
			List<PageWord> list = new ArrayList<PageWord>(keywords.values());
			Collections.sort(list, new Comparator<PageWord>() {

				@Override
				public int compare(PageWord w1, PageWord w2) {
					if (w1.getCount() == w2.getCount())
						return 0;
					else if (w1.getCount() < w2.getCount())
						return 1;
					else
						return -1;
				}
			});
			for (PageWord pw : list) {
				if (pw.getCount() < 5)
					continue;
				// sb.append(pw.getWord() + " (" + pw.getCount() + "), ");
				sb.append(pw.getWord() + ",");
			}
		}

		sb.append("\nLinks:\n");
		if (links != null) {
			for (Link l : links.values()) {
				if (!l.isCitation())
					sb.append(l.getUrl() + "\n");
			}
		}

		sb.append("\nCitations:\n");
		if (links != null) {
			for (Link l : links.values()) {
				if (l.isCitation())
					sb.append(l.getUrl() + "\n");
			}
		}

		return sb.toString();
	}

	public String toStringSummary() {
		StringBuilder sb = new StringBuilder();
		sb.append("Title: " + title);
		sb.append("\nURL: " + url);
		if (links != null)
			sb.append("\nLinks: " + links.size());
		sb.append("\nKeywords: ");
		if (keywords != null) {
			List<PageWord> list = new ArrayList<PageWord>(keywords.values());
			Collections.sort(list, new Comparator<PageWord>() {

				@Override
				public int compare(PageWord w1, PageWord w2) {
					if (w1.getCount() == w2.getCount())
						return 0;
					else if (w1.getCount() < w2.getCount())
						return 1;
					else
						return -1;
				}
			});
			for (PageWord pw : list) {
				if (pw.getCount() < 5)
					continue;
				sb.append(pw.getWord() + " (" + pw.getCount() + "), ");
			}
		}

		sb.append("\n");
		return sb.toString();
	}
}
